package com.atguigu.ggkt.order.api;

import com.atguigu.ggkt.model.order.OrderDetail;
import com.atguigu.ggkt.model.order.OrderInfo;
import com.atguigu.ggkt.order.service.OrderInfoService;
import com.atguigu.ggkt.result.Result;
import com.atguigu.ggkt.utils.AuthContextHolder;
import com.atguigu.ggkt.utils.LogTool;
import com.atguigu.ggkt.vo.order.OrderFormVo;
import com.atguigu.ggkt.vo.order.OrderInfoQueryVo;
import com.atguigu.ggkt.vo.order.OrderInfoVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("api/order/orderInfo")
public class OrderInfoApiController {

    @Autowired
    private OrderInfoService orderInfoService;

    //生成订单方法 
    @PostMapping("submitOrder")
    public Result submitOrder(@RequestBody OrderFormVo orderFormVo) {
        String orderId = orderInfoService.submitOrder(orderFormVo);
        return Result.ok(orderId);
    }

    @ApiOperation(value = "获取")
    @GetMapping("getInfo/{id}")
    public Result getInfo(@PathVariable String id) {
        OrderInfoVo orderInfoVo = orderInfoService.getOrderInfoById(id);
        return Result.ok(orderInfoVo);
    }

    @ApiOperation(value = "获取用户的所有订单")
    @GetMapping("{page}/{limit}")
    public Result listOrder(@PathVariable Long page,
                            @PathVariable Long limit
                            ) {
        //创建page对象
        Long userId = AuthContextHolder.getUserId();
        LogTool.info(this.getClass(), "listOrder userId: "+userId+", page: "+page+", limit: "+limit);
        Page<OrderInfo> pageParam = new Page<>(page,limit);
        Map<String,Object> map = orderInfoService.selectOrderInfoPageMobile(pageParam,userId);
        return Result.ok(map);
    }

    @ApiOperation(value = "获取用户的所有课程")
    @GetMapping("course/{page}/{limit}")
    public Result listCourse(@PathVariable Long page,
                            @PathVariable Long limit
                            ) {
        //创建page对象
        Long userId = AuthContextHolder.getUserId();
        LogTool.info(this.getClass(), "listCourse userId: "+userId+", page: "+page+", limit: "+limit);
        Page<OrderDetail> pageParam = new Page<>(page,limit);
        Map<String,Object> map = orderInfoService.selectOrderCoursePageMobile(pageParam,userId);
        return Result.ok(map);
    }

    @ApiOperation(value = "是否已经购买")
    @GetMapping("isBuy/{courseId}")
    public Result isBuy(@PathVariable Long courseId) {
        Long userId = AuthContextHolder.getUserId();
        boolean bool = orderInfoService.isBuy(courseId, userId);
        return Result.ok(bool);
    }
}
